package com.example.simple_file_chooser;

import android.content.Context;
import android.os.Bundle;

import java.util.Map;
import java.util.TreeMap;



public abstract class AndroidInstance {


    private static Object t;
    FunCallback funcallback;

    public AndroidInstance setEndFunc(FunCallback funcallback) {
        this.funcallback = funcallback;
        return this;
    }

    public AndroidInstance usage(Object mp, Object... p) {
        this.use(mp);
        if (funcallback != null) {
            funcallback.simpleRun(mp);
        }
        return this;
    }

    public abstract AndroidInstance use(Object mp, Object... p);

    Context ctx;

    public AndroidInstance setCtx(Context ctx) {
        this.ctx = ctx;
        return this;
    }


    public static <T extends AndroidInstance> T reservSin() {
        return (T) t;
    }

    public static <T extends AndroidInstance> T reservSin(Class<T> clz, Context ctx) {
        if (t == null) {
            t = get(clz, ctx);
        }
        return (T) t;
    }


    public static <T extends AndroidInstance> T get(Class<T> clz, Context ctx) {
        try {
            T t = (T) clz.newInstance();
            t.setCtx(ctx);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public Context getContext() {
        return ctx;
    }

    Map<String, Object> memberMap = new TreeMap<String, Object>();

    public AndroidInstance build(String memberkey, Object memberval) {
        memberMap.put(memberkey, memberval);
        return this;
    }

    public Object getM(String key) {
        return memberMap.get(key);
    }


    public boolean isEmpty(Object object) {
        return object == null || object.toString().isEmpty();
    }

    public void destroy(String... actions) {
        for (String a : actions) {
            BroadcastBuilder.remove(a, getContext());
        }
    }

    public void preOnCreate(Bundle savedInstanceState) {
    }

    ;
}
